import stacking as sp
import cranesched as cs


CREATE_SP_INSTANCE = "create sp instance from raw csv data"
SOLVE_SP_INSTANCE  = "solve sp instance"
CREATE_CS_INSTANCE = "create cs instance from sp solution"
SOLVE_CS_SLICE     = "solve a slice from a cs instance"

# RUN_SNIPPETS = [CREATE_SP_INSTANCE, SOLVE_SP_INSTANCE]
RUN_SNIPPETS = [SOLVE_CS_SLICE]

# --------------------------------------------------------------------------------------------------
# constants used by the snippets below
# --------------------------------------------------------------------------------------------------
SP_RAWDATA_DIR = "c:\\Users\\Rui\\Projects\\Stacking\\data\\sp\\raw\\"
SP_INSTANCE_DIR = "c:\\Users\\Rui\\Projects\\Stacking\\data\\sp\\instances\\"
SP_SOLUTION_DIR = "c:\\Users\\Rui\\Projects\\Stacking\\data\\sp\\solutions\\"
SP_INSTANCE_EXT = ".sp_instance"
SP_SOLUTION_EXT = ".sp_solution"

CS_INSTANCE_DIR = "c:\\Users\\Rui\\Projects\\Stacking\\data\\cs\\instances\\"
CS_SOLUTION_DIR = "c:\\Users\\Rui\\Projects\\Stacking\\data\\cs\\solutions\\"
CS_INSTANCE_EXT = ".cs_instance"
CS_SOLUTION_EXT = ".cs_solution"

# --------------------------------------------------------------------------------------------------
# parse a csv file containing raw data and create a stacking problem instance
# --------------------------------------------------------------------------------------------------
if CREATE_SP_INSTANCE in RUN_SNIPPETS:
    warehouse, items, actions = sp.loader.load_from_csv(SP_RAWDATA_DIR + "large.csv")
    instance = sp.Instance(items, warehouse)
    instance.save(SP_INSTANCE_DIR + "large" + SP_INSTANCE_EXT)
    
# --------------------------------------------------------------------------------------------------
# load and solve an SP instance, and save the solution
# --------------------------------------------------------------------------------------------------
if SOLVE_SP_INSTANCE in RUN_SNIPPETS:
    from stacking.solvers import HeuristicSolver, heuristics
    
    instance = sp.Instance.load(SP_INSTANCE_DIR + "large" + SP_INSTANCE_EXT)
    solver = HeuristicSolver(heuristics.FO)
    solver.verbose = False
    solution = solver.solve(instance)
    solution.save(SP_SOLUTION_DIR + "fo_large" + SP_SOLUTION_EXT)
    
# --------------------------------------------------------------------------------------------------
# create a CS instance from an SP solution
# --------------------------------------------------------------------------------------------------
if CREATE_CS_INSTANCE in RUN_SNIPPETS:
    sp_solution = sp.Solution.load(SP_SOLUTION_DIR + "fo_large" + SP_SOLUTION_EXT)
    cs_instance = cs.Instance.from_sp(sp_solution)
    cs_instance.save(CS_INSTANCE_DIR + "fo_large" + CS_INSTANCE_EXT)
    
# --------------------------------------------------------------------------------------------------
# load a CS instance, extract a slice from it, solve it, and save the solution
# --------------------------------------------------------------------------------------------------
if SOLVE_CS_SLICE in RUN_SNIPPETS:
    instance = cs.Instance.load(CS_INSTANCE_DIR + "fo_large" + CS_INSTANCE_EXT)
    dfs = cs.solvers.DFS()
    # solution = dfs.solve(instance, subinstance_size=10)
    # solution.save(CS_SOLUTION_DIR + "fo_large_slice0" + CS_SOLUTION_EXT)
    
    
